<!--
  智慧社区管理系统 - 处理弹窗组件
  @author Wu.Liang
  @date 2025-06-26
  @version 3.0 - 根据系统设计调整方案重构
-->
<template>
  <el-dialog
    v-model="dialogVisible"
    title="处理投诉"
    width="600px"
    :close-on-click-modal="false"
    @close="handleClose"
  >
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
    >
      <el-form-item label="处理状态" prop="status">
        <el-select
          v-model="formData.status"
          placeholder="请选择处理状态"
          style="width: 100%"
        >
          <el-option label="已受理" :value="2" />
          <el-option label="处理中" :value="3" />
          <el-option label="已完成" :value="4" />
          <el-option label="已关闭" :value="5" />
        </el-select>
      </el-form-item>
      
      <el-form-item label="处理内容" prop="content">
        <el-input
          v-model="formData.content"
          type="textarea"
          :rows="4"
          placeholder="请详细描述处理过程和结果"
          maxlength="500"
          show-word-limit
        />
      </el-form-item>
      
      <el-form-item label="备注" prop="remark">
        <el-input
          v-model="formData.remark"
          type="textarea"
          :rows="2"
          placeholder="请输入备注信息（可选）"
          maxlength="200"
          show-word-limit
        />
      </el-form-item>
    </el-form>
    
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="handleSubmit" :loading="loading">
          确认处理
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, reactive, watch, computed } from 'vue'
import type { FormInstance, FormRules } from 'element-plus'
import type { Complaint } from '../types/complaint'

// 作者: Wu.Liang

// Props
interface Props {
  modelValue: boolean
  complaintData: Complaint | null
  loading?: boolean
}

const props = withDefaults(defineProps<Props>(), {
  loading: false
})

// Emits
const emit = defineEmits<{
  'update:modelValue': [value: boolean]
  'confirm': [data: any]
}>()

// 响应式数据
const formRef = ref<FormInstance>()

// 表单数据
const formData = reactive({
  status: 3,
  content: '',
  remark: ''
})

// 表单验证规则
const formRules: FormRules = {
  status: [
    { required: true, message: '请选择处理状态', trigger: 'change' }
  ],
  content: [
    { required: true, message: '请输入处理内容', trigger: 'blur' },
    { min: 10, max: 500, message: '内容长度在 10 到 500 个字符', trigger: 'blur' }
  ]
}

// 计算属性
const dialogVisible = computed({
  get: () => props.modelValue,
  set: (value) => emit('update:modelValue', value)
})

// 监听器
watch(() => props.modelValue, (newVal) => {
  if (newVal) {
    resetForm()
  }
})

// 重置表单
const resetForm = () => {
  Object.assign(formData, {
    status: 3,
    content: '',
    remark: ''
  })
  formRef.value?.clearValidate()
}

// 提交表单
const handleSubmit = async () => {
  if (!formRef.value) return
  
  try {
    await formRef.value.validate()
    
    const submitData = {
      status: formData.status,
      content: formData.content,
      remark: formData.remark || undefined
    }
    
    emit('confirm', submitData)
  } catch (error) {
    console.error('表单验证失败:', error)
  }
}

// 关闭弹窗
const handleClose = () => {
  dialogVisible.value = false
}
</script>

<style scoped>
.dialog-footer {
  text-align: right;
}
</style> 